// 登陆操作
var login = {
    check: function () {
        var user = $('#user').val();
        var pass = $('#pass').val();

        if (!user) {
            return dialog.error('用户名不能为空');
        }
        if (!pass) {
            return dialog.error('密码不能为空');
        }

        var url = SCOPE.login_url;
        var data = {'user': user, 'pass': pass};
        $.post(url, data, function (result) {
            if (result.status == 0) {
                return dialog.error(result.message);
            }
            if (result.status == 1) {
                return dialog.success(result.message, SCOPE.index_url);
            }
        }, 'json');
    }
};

//// 点击发表弹出层
//$('.btn_menu').click(function () {
//    dialog.openContent();
//});

$('.btn_menu').click(function () {
    dialog. openContent(SCOPE.menu_dialog);
});

/**
 * 提交form表单操作
 */
$("#btn_form").click(function () {
    var title = $('#nike').val();
    var thumb = $('#thumb').val();
    var postData = {'title': title, 'thumb': thumb};


    // 将获取到的数据post给服务器
    var url = SCOPE.add_url;

    $.post(url, postData, function (result) {
        if (result.status == 1) {
            //成功
            return dialog.parentIframe(result.message, SCOPE.menu_index_url);

        } else if (result.status == 0) {
            // 失败
            return dialog.error(result.message);
        }
    }, "JSON");
});




// 菜单跳转
$('.menu_model').click(function () {
    location.href = SCOPE.menu_url;
});

// 修改链接跳转
$('.menu_edit').click(function () {
    var id = $(this).attr('data-id');
    dialog.openContent(SCOPE.menu_edit + '/id/' + id);
});


 // 提交form表单操作
 //$("#save_form").click(function () {
 //    var title = $('#nike').val();
 //    var thumb = $('#thumb').val();
 //
 //    var postData = {'title': title, 'thumb': thumb};
 //
 //
 //    // 将获取到的数据post给服务器
 //    var url = SCOPE.add_url;
 //
 //    $.post(url, postData, function (result) {
 //        if (result.status == 1) {
 //            //成功
 //            return dialog.success(result.message, SCOPE.index_url);
 //
 //        } else if (result.status == 0) {
 //            // 失败
 //            return dialog.error(result.message);
 //        }
 //    }, "JSON");
 //});


// 修改图片按钮 隐藏
$('#filePicker').click(
    function () {
        $('#form_data > img').hide();
    }
);


$("#save_form").click(function () {
    var title = $('#nike').val();
    var thumb = $('#thumb').val();
    var menu_id = $('#menu_id').val();
    var postData = {'menu_id': menu_id, 'title': title, 'thumb': thumb};


    // 将获取到的数据post给服务器
    var url = SCOPE.save_url;

    $.post(url, postData, function (result) {
        if (result.status == 1) {
            //成功
            return dialog.parentIframe(result.message, SCOPE.menu_index_url);

        } else if (result.status == 0) {
            // 失败
            return dialog.error(result.message);
        }
    }, "JSON");
});


// 菜谱 删除单条数据

// 修改链接跳转
$('.menu_del').click(function () {
    var id = $(this).attr('data-id');
    var url = SCOPE.menu_del;
    $.post(url, {'id': id}, function (result) {
        if (result.status == 1) {
            return dialog.success(result.message, SCOPE.menu_url);
        }
        if (result.status == 0) {
            return dialog.error(result.message);
        }
    }, 'json');

});

// 用户管理跳转
$('.menu_admin').click(function () {
    location.href = SCOPE.admin_url;
});

// 添加用户管理跳转
$('.add_user').click(
    function () {
        location.href = SCOPE.add_user_url;
    }
);


// 用户表 添加新用户
$('.add_user_admin').click(function () {
    var url = SCOPE.admin_add_url;
    var username = $('#username').val();
    var password = $('#password').val();
    if (!username) {
        return dialog.error('内容不能为空');
    }
    if (!password) {
        return dialog.error('密码不能为空');
    }

    $.post(url, {'username': username, 'password': password}, function (result) {
        if (result.status == 1) {
            return dialog.success(result.message, SCOPE.admin_url);
        }
        if (result.status == 0) {
            return dialog.error(result.message);
        }

    }, 'json');
});

// 用户表删除

$('.user_del').click(function () {
    var id = $(this).attr('data-id');
    var url = SCOPE.admin_del;
    $.post(url, {'id': id}, function (result) {
        if (result.status == 1) {
            return dialog.success(result.message, SCOPE.admin_url);
        }
        if (result.status == 0) {
            return dialog.error(result.message);
        }
    }, 'json');

});

// 用户表修改
$(".save_user_admin").click(function () {
    var username = $('#username').val();
    var password = $('#password').val();
    var user_id = $('#user_id').val();
    var postData = {'user_id': user_id, 'user_name': username, 'password': password};


    // 将获取到的数据post给服务器
    var url = SCOPE.admin_save;

    $.post(url, postData, function (result) {
        if (result.status == 1) {
            //成功
            return dialog.success(result.message, SCOPE.admin_url);

        } else if (result.status == 0) {
            // 失败
            return dialog.error(result.message);
        }
    }, "JSON");
});
$('.user_edit').click(function () {
        var id = $(this).attr('data-id');
        location.href = SCOPE.admin_edit+'/id/'+id;
});

//退出登录

$('.out').click(
    function(){
       location.href = '/manage/admin.php/Index/loginout';
    }
);

//进入前台
$('.home_index').click(
    function(){
        location.href = '/home.php/Index/index';
    }
);